

from datetime import datetime
from xml.etree import ElementTree
import sqlite3
import os

class TwitterStatuses:

	def __init__(self,filepath,createnew=True):
		exists = os.path.exists(filepath)
		if createnew==False:
			if exists:
				self.connection=sqlite3.connect(filepath)
			else:
				self.connection=sqlite3.connect(filepath)
				createPostsSQL = r'CREATE TABLE Data (Data)'		
				self.connection.execute(createPostsSQL)
		else:
			if exists:
				os.remove(filepath)
			self.connection=sqlite3.connect(filepath)
			createPostsSQL = r'CREATE TABLE Data (Data)'		
			self.connection.execute(createPostsSQL)

	def persist(self,item):
		self.connection.execute('INSERT INTO Data VALUES (:data)',{'data':ElementTree.tostring(item.toxml())})
		self.connection.commit()
	
	def __del__(self):
		self.connection.close()



class Tweet:
	def __init__(self):
		
		self.ReplyToStatus = u''
		
		self.Content = u''
		
		self.Permalink = u''
		
		self.Author = u''
		
		self.User = User()
		
		self.AuthorLink = u''
		
		self.Source = u''
		
		self.Title = u''
		
		self.Published = datetime(1970,1,1)
		
		self.Truncated = u''
		
		self.ReplyToUser = u''
		

		

	def toxml(self):
		element = ElementTree.Element('Tweet')
		
		ElementTree.SubElement(element,'ReplyToStatus').text = self.ReplyToStatus
		
		ElementTree.SubElement(element,'Content').text = self.Content
		
		ElementTree.SubElement(element,'Permalink').text = self.Permalink
		
		ElementTree.SubElement(element,'Author').text = self.Author
		
		ElementTree.SubElement(element,'User').append(self.User.toxml())
		
		ElementTree.SubElement(element,'AuthorLink').text = self.AuthorLink
		
		ElementTree.SubElement(element,'Source').text = self.Source
		
		ElementTree.SubElement(element,'Title').text = self.Title
		
		ElementTree.SubElement(element,'Published').text = self.Published.isoformat()
		
		ElementTree.SubElement(element,'Truncated').text = self.Truncated
		
		ElementTree.SubElement(element,'ReplyToUser').text = self.ReplyToUser
		

		
		
		return element

class User:
	def __init__(self):
		
		self.Name = u''
		
		self.Location = u''
		
		self.URL = u''
		
		self.ScreenName = u''
		
		self.ID = 0
		
		self.ProfileImage = u''
		
		self.Followers = 0
		
		self.Description = u''
		

		

	def toxml(self):
		element = ElementTree.Element('User')
		
		ElementTree.SubElement(element,'Name').text = self.Name
		
		ElementTree.SubElement(element,'Location').text = self.Location
		
		ElementTree.SubElement(element,'URL').text = self.URL
		
		ElementTree.SubElement(element,'ScreenName').text = self.ScreenName
		
		ElementTree.SubElement(element,'ID').text = str(self.ID)
		
		ElementTree.SubElement(element,'ProfileImage').text = self.ProfileImage
		
		ElementTree.SubElement(element,'Followers').text = str(self.Followers)
		
		ElementTree.SubElement(element,'Description').text = self.Description
		

		
		
		return element



